var GlobalPriority = @options("priority") || [];

module.exports = function(lab) {
    return {
        @getMemberPushChannel: function(the_member_id) {

            return @.async(function() {

                lab.slime.member.@getMember(the_member_id).pipe(this);

            }).then(function (member, member_id){

                lab.slime.member.@getSourcesByOUID(member_id.ouid).then(function (sources) {
                    
                    lab.slime_subscribe.core.@getSettings(member_id.ouid).then(function (settings) {

                        var priority = settings.priority &&　settings.priority.length ? settings.priority : sources;

                        this.next(member_id, priority.filter(function (channel) {
                            return sources.indexOf(channel) > -1;
                        }));
    
                    }).pipe(this);

                }).pipe(this);

            }).then(function(member_id, priority) {

                if (priority.length) {
                    
                    @.async(function () {

                        lab.sqlite.find("slime_member_subscriptions", {
                            member_id  : member_id.id,
                            channel_id : ["in", priority],
                            is_removed : false
                        }).pipe(this);

                    }).then(function ({records}) {

                        this.pool.subscriptions = records;
                        
                        this.pool.priority = priority.filter(function (channel) {
                            return !records.find((channel_info) => channel_info.channel_id == channel);
                        });

                        lab.sqlite.find("slime_member_recommands", {
                            member_id: member_id.id,
                            channel_id: ["in", this.pool.priority],
                            is_removed: false
                        }).pipe(this);

                    }).then(function ({records}) {

                        var recommands = this.pool.recommands = records;
                        var subscriptions = this.pool.subscriptions;

                        this.pool.priority = priority.filter(function (channel) {
                            return !records.find((channel_info) => channel_info.channel_id == channel);
                        });
                        
                        @.async.all(this.pool.priority,function (channel) {
                            switch (channel.split(".")[0]){
                                case "mail" : {

                                    if (member_id.email){
                                        this.next({
                                            channel_id: channel,
                                            channel_member_id: member_id.email
                                        });
                                    } else {
                                        this.next();
                                    }
                                    break;
                                }
                                case "sms" : {
                                    if (member_id.phone){
                                        this.next({
                                            channel_id: channel,
                                            channel_member_id: member_id.phone
                                        });
                                    } else {
                                        this.next();
                                    }
                                    break;
                                }
                                default : {

                                    @dump(member_id);

                                    var idField = channel.split(".").join("_") + "_id";
                                    @dump(idField);
                                    if (lab.models.slime_member_ids[idField] && member_id[idField]) {

                                        return lab.create("slime_member_recommands", {
                                            member_id: the_member_id,
                                            channel_id: channel,
                                            channel_member_id: member_id[idField],
                                            is_removed: false,
                                            create_date: Date.now(),
                                            last_update: Date.now()
                                        });

                                    } else {
                                        this.next()
                                    }
                                }
                            }
                        }).then(function (rest_channels) {
                            this.next(rest_channels.filter((channel) => channel));
                        }).pipe(this);

                    }).then(function (rest_recommands) {
                        
                        this.next(rest_recommands.concat(this.pool.subscriptions).concat(this.pool.recommands).sort(function (channelA, channelB) {
                            return priority.indexOf(channelA.channel_id) - priority.indexOf(channelB.channel_id);
                        }));

                    }).pipe(this);
                } else {
                    this.next([]);
                }
            })
        }
    }
}